<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        .hide {
            display: none;
        }

        .c1 {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(0, 0, 0, .5);
            z-index: 2;
        }

        .c2 {
            background-color: white;
            position: fixed;
            width: 400px;
            height: 300px;
            top: 50%;
            left: 50%;
            z-index: 3;
            margin-top: -150px;
            margin-left: -200px;
            padding-top: 2%;
            padding-left: 2%;
        }

        .c3 {
            background-color: white;
            position: fixed;
            width: 400px;
            height: 300px;
            top: 50%;
            left: 50%;
            z-index: 3;
            margin-top: -150px;
            margin-left: -200px;
            padding-top: 2%;
            padding-left: 2%;
        }

        #modal p {
            margin-left: 80px;
        }

        input::placeholder {
            color: #D0D0D0;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            不合格品保存
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">

        <div id="citemList" class="aui-content">
            <div class="aui-form" id="citemForm">

            </div>
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">产品条码</div>
                        <div class="aui-list-item-input">
                            <input id="barcode"></input>
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">当前工序</div>
                        <div class="aui-list-item-input" onclick="goSelectOpCode()">
                            <input id="opCode" readonly></input>
                        </div>
                    </div>
                </li>
            </ul>
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">缺陷类别</div>
                        <div class="aui-list-item-input">
                            <select id="qualchkitemType">
                            </select>
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">缺陷内容</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择缺陷内容" id="qualchkitem" value="" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectqualchkitem()"></i>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 45%;">返修工艺路线</div>
                        <div class="aui-list-item-input">
                            <input type="text" id="routeCode" value="" />
                        </div>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">返修类型</div>
                        <select id="uda1">
                        </select>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">班&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp次</div>
                        <div class="aui-list-item-input">
                            <select id="shiftCode">
                            </select>
                        </div>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">备注</div>
                        <div class="aui-list-item-input">
                            <input id="remark"></input>
                        </div>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">蓝牙设备</div>
                        <select id="bluetooth">
                        </select>
                    </div>
                </li>
            </ul>

            <div class="aui-content" style="background-color: #ffffff">
                <ul class="aui-list-view aui-grid-view" id="imgslist">
                    <li class="aui-list-view-cell aui-img aui-col-xs-4" id="addpic" onclick="imgClick()">
                        <img class="aui-img-object" src="../image/add.jpg">
                    </li>
                </ul>
            </div>
        </div>

    </div>
    <div class=' aui-btn aui-btn-info aui-btn-block' onclick='doSave()'>保存
    </div>
    <div style="width:100%;margin-top:2%;"></div>
    <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/hammer.min.js"></script>
<script type="text/javascript">

    var qualchkitemCode = "";
    var routeCode = "";
    var routeName = "";
    var routeId = "";
    var qualchkitemName = "";
    var qualchkitemGid = "";

    var barcode = "";
    var opCode = "";
    var opName = "";
    var opid = "";
    var equipUniqueId = "";
    var flag = "N";

    var uiMediaScanner = null;
    var imageFilter = null;
    var imageBrowser = null;
    //初始化
    apiready = function () {
        //获取蓝牙列表
        getBluetoothCombox();

        $("#barcode").focus();

        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');



        // 引入多选模块
        uiMediaScanner = api.require('UIMediaScanner');
        // 引入过滤压缩模块
        imageFilter = api.require("imageFilter");
        // 引入图片浏览模块
        imageBrowser = api.require('imageBrowser');

        //查询缺陷类别下拉框
        doSearchUqcqualchkitem();
        //返修类型下拉框
        selectUda1();
          //2:查询班次下拉框
          doSearchTeamShift();

    };

       //班组下拉框
       function doSearchTeamShift() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyTeam";
        var data = {
            values: {
                workCenterCode: '',
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    //班次赋值
                    var listShift = eval(myobj.listShift);
                    var htmlShift = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < listShift.length; i++) {
                        htmlShift += "<option value='" + listShift[i].code + "'>" + listShift[i].name + "</option>";
                    }
                    $api.html($api.byId("shiftCode"), htmlShift);
                    $("#shiftCode").val(api.pageParam.shiftCode);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }


    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.connectBluetooth({
            address: address,
        }, function (ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.searchBluetooth({}, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }





    //产品条码监听回车事件
    $("#barcode").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#opCode").focus();
        }
    });

    //查询待检区库位下拉框
    function selectUda1() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxListBycode";
        var data = {
            values: {
                code: "REPAIR_TYPE",
                removeDefault: "Y",
                un: un
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                if (map.code == "0") {
                    var list = eval(map.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }
                    $api.html($api.byId("uda1"), html);
                    //失败
                } else if (map.code == "1") {
                    api.toast({
                        msg: map.message,
                        duration: 3000
                    });
                }
            } else {
                alert("连接超时，请耐心等待");
            }
        });
    }



    //保存
    function doSave() {

        var barcode_web = document.getElementById('barcode').value;

        if (barcode_web == null || barcode_web == "") {

            api.toast({
                msg: "请先扫描产品条码！",
                duration: 3000
            });
            return
        }

        var opCode_web = document.getElementById('opCode').value;


        if (opCode_web == null || opCode_web == "") {

            api.toast({
                msg: "请先选择工序！",
                duration: 3000
            });
            return
        }

        var type = $("#qualchkitemType").val();

        if (type == null || type == "") {

            api.toast({
                msg: "请先选择缺陷类别！",
                duration: 3000
            });
            return
        }

        var qualchkitem = document.getElementById('qualchkitem').value;

        if (qualchkitem == null || qualchkitem == "") {

            api.toast({
                msg: "请先选择缺陷内容！",
                duration: 3000
            });
            return
        }
        console.log(routeCode)

        if (routeCode === null || routeCode === "") {

            api.toast({
                msg: "返修工艺路线未配置！",
                duration: 3000
            });
            return
        }

        var shiftCode = $("#shiftCode").val();
        if (shiftCode == null || shiftCode == "" || shiftCode == undefined) {
            api.toast({
                msg: "请选择班次!",
                duration: 3000
            });
            return;
        }


        var uda1 = document.getElementById('uda1').value;

        if (uda1 == null || uda1 == "") {

            api.toast({
                msg: "请先选择返修类别！",
                duration: 3000
            });
            return
        }
        var jjFlag = 'Y'//是否更新人员过件计数表

        if (barcode != null && barcode != undefined) {
            jjFlag = 'N'
        }



        var remark = document.getElementById('remark').value;

        //获取imgslist中所有的img，保存图片和异常业务记录的关系
        var imgs = document.getElementsByTagName('img');
        var names = [];

        for (var i = 0; i < imgs.length; i++) {
            var img = imgs[i];
            var srcStr = img.getAttribute('src');
            var arrs = srcStr.split('\/');
            var nameStr = arrs[arrs.length - 1];
            //alert(nameStr);
            if ("add.jpg" != nameStr) {
                names[i] = nameStr;
            }
        }
        var picNames = JSON.parse(JSON.stringify(names));
        if (picNames == '' || picNames == null) {
            api.toast({
                msg: "请先上传返修图片！",
                duration: 3000
            });
            return
        }

        var address = $("#bluetooth").val();

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/nonConformSaveFx";
        var data = {
            values: {
                remark: remark,
                barcode: barcode_web,
                opCode: opCode_web,
                qualchkitemType: type,
                jjFlag: jjFlag,
                routeId: routeId,
                routeCode: routeCode,
                routeName: routeName,
                qualchkitem: qualchkitem,
                qualchkitemCode: qualchkitemCode,
                shiftCode: shiftCode,
                equipUniqueId: equipUniqueId,
                un: un,
                repairType: uda1,
                picNames: picNames,
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                console.log(ret);
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                clearContent();
                if (myobj.code == 0) {
                    alert('保存成功！')
                    // 
                    if (api.pageParam.barcode == undefined) {
                        //api.closeWin({});
                    } else {
                        api.closeToWin({ name: api.pageParam.name });
                        api.execScript({
                            name: api.pageParam.name,
                            script: 'refreshData()'
                        });
                    }

                    var json = myobj.json;
                    //调用PDA打印
                    printPDA(json, address);

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });


    }

      //打印
      function printPDA(json, address) {
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.printerXc({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //查询缺陷类别下拉框
    function doSearchUqcqualchkitem() {
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                tablename: "qc_bd_chk_item_type",
                column_name: "name",
                column_value: "code",
                // condition: " ext01 = 'LLJC'",
                un: 'combox'
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            async: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);

                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("qualchkitemType"), html);
            }

        });
    }

    function clearContent() {
        //document.getElementById('barcode').value = "";

        //document.getElementById('opCode').value = "";

        $("#qualchkitemType").val("");
        $("#barcode").val("");
        $("#routeCode").val("");
        $("#opCode").val("");
        opCode = '';
        opid = '';
        opName = '';
        routeCode = '';
        routeId = '';
        routeName = '';
        document.getElementById('opCode').value = "";
        document.getElementById('qualchkitem').value = "";
        document.getElementById('barcode').value = "";
        document.getElementById('remark').value = "";

        removeImg();

        //获取imgslist中所有的img，保存图片和异常业务记录的关系
        // document.getElementsByTagName('img') = "";

        // var oldnode = document.getElementsByTagName('img')[0];

        //  oldnode.parentNode.removeChild(oldnode)
    }

    //保存成功后，清除图片
    function removeImg() {
        var imgs = document.getElementsByTagName('img');
        console.log(imgs.length + "0000")
        for (var i = 0; i < imgs.length; i++) {
            var img = imgs[i];
            console.log(img + "1111" + i)
            var srcStr = img.getAttribute('src');
            var obj = $("#imgslist img[src='" + srcStr + "']").parent("li");
            var arrs = srcStr.split('\/');
            var nameStr = arrs[arrs.length - 1];
            console.log(nameStr + "222" + i)
            if ("add.jpg" != nameStr) {
                console.log(nameStr + "333" + i)
                $(obj).remove();
            }
        }
    }

    //产线选择后回调函数
    function setQualchkitem(sysid, code, name, routeCodeFx, routeNameFx, routeIdFx) {
        qualchkitemGid = sysid;
        qualchkitemCode = code;
        qualchkitemName = name;
        if (routeCodeFx != 'null' && routeNameFx != 'null' && routeIdFx != 'null') {
            routeCode = routeCodeFx;
            routeName = routeNameFx;
            routeId = routeIdFx;
            document.getElementById('routeCode').value = routeCode + ':' + routeName;
            console.log(routeCode + ':' + routeName)
        } else {
            routeCode = '';
            routeName = '';
            routeId = '';
            document.getElementById('routeCode').value = '';
        }
        document.getElementById('qualchkitem').value = code+':'+name;
    }

    function setOpCode(id, code, name) {
        opid = id;
        opCode = code;
        opName = name;
        document.getElementById('opCode').value = code;
    }

    //缺陷内容下拉选
    function goSelectqualchkitem() {
        var type = $("#qualchkitemType").val();
        if (type) {
            api.openWin({
                name: 'usmSelectQualchkitemFx',
                url: 'usmSelectQualchkitemFx.html',
                pageParam: {
                    code: type,
                    name: "nonConformProductFx",
                }
            });

        } else {
            api.toast({
                msg: "请先选择缺陷类别！",
                duration: 3000
            });
        }

    }

    function goSelectOpCode() {

        if (opCode != "" && opCode != undefined) {
            return
        }
        var barcode_web = document.getElementById('barcode').value;
        if (barcode_web) {

            api.openWin({
                name: 'paOpCode',
                url: 'paOpCode.html',
                pageParam: {
                    code: barcode_web,
                    name: "nonConformProductFx",
                }
            });

        } else {
            api.toast({
                msg: "请先扫描产品条码",
                duration: 3000
            });
        }
    }


    //返回按钮
    function closeWin() {
        // 
        if (api.pageParam.barcode == undefined) {
            api.closeWin({});
        } else {
            api.closeToWin({ name: api.pageParam.name });
            api.execScript({
                name: api.pageParam.name,
                script: 'refreshData()'
            });
        }
    }

    function imgClick() {
        // 获取图片宽高
        var normalW = $("#addpic img").width();
        api.actionSheet({
            title: '选择图片来源',
            buttons: ['照相机', '浏览相册']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            switch (index) {
                // 拍照
                case 1:
                    // 打开拍照
                    api.getPicture({
                        sourceType: "camera",
                        encodingType: "jpg",
                        destinationType: "url",
                        mediaValue: "pic",
                        quality: 50,
                        saveToPhotoAlbum: true
                    }, function (ret, err) {
                        if (ret && ret.data) {
                            // 拍照返回的本地路径
                            var returnUrl = ret.data;
                            // 图片压缩
                            imgCompress(returnUrl, 0.5, 0.5, getExt(returnUrl), function (compressImg) {
                                var showImgHtml = "<li class='aui-list-view-cell aui-img aui-col-xs-4  aui-padded-5' style='margin-left: 5px;width:" + normalW + "px;height:" + normalW + "px;' onclick='lookImg(\"" + compressImg + "\")'><img class='aui-img-object' src='" + compressImg +
                                    "' style='width:" + normalW + "px;height:" + normalW + "px;'></li>";
                                // 追加图片
                                $("#addpic").before(showImgHtml);
                                // ########################################  绑定长按事件 ########################
                                addPress($("#imgslist img[src='" + compressImg + "']").parent("li"));

                                //上传图片
                                uploadFile(compressImg);
                            });
                        } else {
                            api.toast({
                                msg: '没有选择，或者选择出错'
                            });
                        }
                    });
                    break;
                // 打开图片选择器
                case 2:
                    uiMediaScanner.open({
                        type: 'picture',
                        column: 4,
                        classify: true,
                        max: 5,
                        sort: {
                            key: 'time',
                            order: 'desc'
                        },
                        texts: {
                            stateText: '已选*项',
                            cancelText: '取消',
                            finishText: '完成'
                        },
                        styles: {
                            bg: '#fff',
                            mark: {
                                icon: '',
                                position: 'bottom_left',
                                size: 20
                            },
                            nav: {
                                bg: '#b23e4b',
                                stateColor: '#fff',
                                stateSize: 18,
                                cancelBg: 'rgba(0,0,0,0)',
                                cancelColor: '#fff',
                                cancelSize: 18,
                                finishBg: 'rgba(0,0,0,0)',
                                finishColor: '#fff',
                                finishSize: 18
                            }
                        }
                    }, function (ret) {
                        if (ret) {
                            if (ret.list) {
                                for (var i = 0; i < ret.list.length; i++) {
                                    var selectImg = ret.list[i];
                                    // 获取图片的路径
                                    var selectimgPath = selectImg.path;
                                    var selectimgThumbPath = selectImg.thumbPath;
                                    // 图片压缩
                                    imgCompress(selectimgPath, 0.5, 0.5, selectImg.suffix, function (compressImg) {
                                        var showImgHtml = "<li class='aui-list-view-cell aui-img aui-col-xs-4 aui-padded-5' style='margin-left: 5px;width:" + normalW + "px;height:" + normalW + "px;'  onclick='lookImg(\"" + compressImg + "\")'><img class='aui-img-object' src='" + compressImg +
                                            "' style='width:" + normalW + "px;height:" + normalW + "px;'></li>";
                                        // 追加图片
                                        $("#addpic").before(showImgHtml);
                                        // ########################################  绑定长按事件 ########################
                                        addPress($("#imgslist img[src='" + compressImg + "']").parent("li"));

                                        // ################### 上传图片 #########################
                                        uploadFile(compressImg);
                                    });
                                }
                            }
                        }
                    });
                    break;
            }
        });
    }

    // 图片压缩
    // imgsrc：源图片的路径
    // quality：图片压缩质量，一般建议0.5
    // scale：图片压缩比例，也是建议0.5
    // ext：源图片拓展名
    // callback：转换成功之后回调函数
    function imgCompress(imgsrc, quality, scale, ext, callback) {
        // 压缩文件的保存目录
        var savePath = api.cacheDir + "/" + getNowFormatDate() + "/";
        // 压缩文件生成的随机文件名称
        var savename = NewGuid() + "." + ext;
        imageFilter.compress({
            img: imgsrc,
            quality: quality,
            scale: quality,
            save: {
                album: false,
                imgPath: savePath,
                imgName: savename
            }
        }, function (ret, err) {
            if (ret) {
                callback(savePath + savename);
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    // 打开图片浏览
    // imgs：需要预览的图片集合
    function openImageBrowser(imgs) {
        imageBrowser.openImages({
            imageUrls: imgs,
            showList: false,
            activeIndex: 0
        });
    }

    // 获取当前的时间，拼接成2015-11-09这样的格式，主要用于对图片进行时间分类
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate
        return currentdate;
    }

    // 获取文件拓展名
    function getExt(fileName) {
        return fileName.substring(fileName.lastIndexOf('.') + 1);
    }

    // 生成guid,主要用于生成随机文件名
    function NewGuid() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }

        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }

    // 添加长按方法
    function addPress(obj, index) {
        // 获取目前长按的对象
        var hammertime = new Hammer(obj[0]);
        // 绑定长按对象
        hammertime.on("press", function (e) {
            api.confirm({
                title: '温馨提示',
                msg: '您确定要删除该图片吗？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    // 移除自己
                    $(obj).remove();
                    api.toast({
                        msg: '删除成功！'
                    });
                }
            });
        });
    }

    // 上传图片
    // url：上传的url地址
    // data：上传的文件
    // callback：上传成功返回地址
    function uploadFile(data) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uploadPhotosAPP";
        var data = {
            values: {
                un: un
            },
            files: {
                file: data
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == "0") {
                    console.log("上传成功");
                } else if (myobj.code == "1") {
                    alert("上传失败");
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    // 为图片添加点击预览功能,排除添加按钮
    function lookImg(compressImg) {
        // 定义一个数组，存储需要预览的图片
        var browerImgs = [];
        // 调用图片预览函数
        browerImgs.push(compressImg);
        openImageBrowser(browerImgs);
    }

</script>

</html>